package com.arpa.wms.controller;

import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.ShipmentWarehouseValidate;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.oms.domain.entity.PurchaseItem;
import com.arpa.wms.domain.consts.PropertyConst;
import com.arpa.wms.domain.consts.TipConst;
import com.arpa.wms.domain.dto.BrandDTO;
import com.arpa.wms.domain.dto.FaceBillBalanceDTO;
import com.arpa.wms.domain.entity.Brand;
import com.arpa.wms.domain.entity.Carrier;
import com.arpa.wms.domain.entity.FaceBillBalance;
import com.arpa.wms.domain.entity.Inventory;
import com.arpa.wms.domain.enums.StatusEnum;
import com.arpa.wms.domain.vo.BrandVO;
import com.arpa.wms.domain.vo.FaceBillBalanceVO;
import com.arpa.wms.service.FaceBillBalanceService;
import com.arpa.wms.service.IBrandService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 品牌 Controller
 * </p>
 *
 * @author LEO
 * @since 2020-08-03
 */
@Api(tags = "面单余额")
@RestController
@RequestMapping(value="/faceBillBalance", produces = "application/json;charset=UTF-8")
@Log4j2
public class FaceBillBalanceController {

   @Autowired
   private FaceBillBalanceService faceBillBalanceService;

    /**
     * 获取 面单余额 列表
     *
     * @param faceBillBalanceDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 面单余额 列表", notes = "获取 面单余额 列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = BrandVO.class))
    @NeedLogin
    @GetMapping
    public Result list(FaceBillBalanceDTO faceBillBalanceDTO, HttpServletRequest request) {
        try {
            //校验授权仓库code和货主code不能为空
            ShipmentWarehouseValidate.validateShipmentCodesAndWarehouseCodes();
            //登录仓库授权的货主过滤
//            brandDTO.setShipmentCodes(UserUtil.getAuthorizeShipmentCodes());
            //所属机构的过滤条件
            faceBillBalanceDTO.setGroupCode(UserUtil.getBranchCode());

            List<FaceBillBalanceVO> brandVOList = faceBillBalanceService.queryList(faceBillBalanceDTO);

            return Result.ok().data(brandVOList);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加 面单余额
     *
     * @param faceBillBalanceDTO faceBillBalanceDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "添加 面单余额 ", notes = "添加 面单余额 ")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping
    public Result create(@RequestBody FaceBillBalanceDTO faceBillBalanceDTO, HttpServletRequest request) {
        faceBillBalanceDTO.setGroupCode(UserUtil.getBranchCode());
        try {
            faceBillBalanceService.saveFaceBillBalance(faceBillBalanceDTO);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
    * 获取面单余额详情
    *
    * @param code
    * @param request
    * @return
    */
    @ApiOperation(value = "获取面单余额详情", notes = "获取面单余额详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "code", value = "面单余额code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = BrandVO.class))
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result detail(@PathVariable String code, HttpServletRequest request) {
        try {
            FaceBillBalance entity = faceBillBalanceService.getOne(new QueryWrapper<FaceBillBalance>().lambda()
                    .eq(FaceBillBalance::getCode,code).eq(FaceBillBalance::getGroupCode,UserUtil.getBranchCode()));
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }



    /**
     * 修改面单余额
     * @param entity 面单余额
     * @param request
     * @return
     */
    @ApiOperation(value = "修改面单余额", notes = "修改面单余额")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping
    public Result update(@RequestBody FaceBillBalanceDTO entity,HttpServletRequest request) {

        try{
            if(StrUtil.isBlank(entity.getCode())){
                return Result.error(TipConst.CODE_NOT_EMPTY);
            }
            int updateNum = faceBillBalanceService.updateByCode(entity);
            if(updateNum == 0){
                return Result.error("没有更新数据！code:"+entity.getCode());
            }else{
                return Result.ok("修改成功,更新"+updateNum+"条数据！");
            }
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    @ApiOperation(value = "删除面单余额", notes = "删除面单余额", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "codes", value = "面单余额code", paramType = "path", dataType = "String")
    })
    @NeedLogin
    @DeleteMapping(value = "/{codes}")
    public Result delete(@PathVariable List<String> codes, HttpServletRequest request) {

        try {
            faceBillBalanceService.update(new UpdateWrapper<FaceBillBalance>().lambda().in(FaceBillBalance::getCode, codes)
                    .eq(FaceBillBalance::getGroupCode,UserUtil.getBranchCode())
                    .set(FaceBillBalance::getDeleted,"1"));
            return Result.ok();
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

}
